package cn.iocoder.yudao.module.shop.admin.dao;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.business.basic.dao.mapper.CoreMapper;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreLambdaQueryWrapper;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.module.shop.admin.pojo.po.ShopUser;
import cn.iocoder.yudao.module.shop.admin.pojo.vo.ShopUserPageReqVO;
import jakarta.annotation.Nullable;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
import java.util.Objects;

/**
 * 店铺商户 Mapper
 *
 * @author 山野羡民（1032694760@qq.com）
 */
@Mapper
public interface ShopUserMapper extends CoreMapper<ShopUser> {

    @DataPermission(enable = false) // 指定店铺编号查询，需忽略店铺的数据权限
    default PageResult<ShopUser> selectPage(ShopUserPageReqVO reqVO) {
        return selectPage(reqVO, new CoreLambdaQueryWrapper<ShopUser>()
                .eqIfPresent(ShopUser::getShopId, reqVO.getShopId())
                .eqIfPresent(ShopUser::getMemberUserId, reqVO.getMemberUserId())
                .betweenIfPresent(ShopUser::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ShopUser::getId));
    }

    @DataPermission(enable = false) // 指定店铺编号查询，需忽略店铺的数据权限
    default ShopUser selectOne(Long userId, Integer userType, @Nullable Long shopId) {
        List<ShopUser> list = selectList(userId, userType, shopId);
        if (CollUtil.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    @DataPermission(enable = false) // 指定店铺编号查询，需忽略店铺的数据权限
    default Long selectShopIdByAdminUserId(Long adminUserId) {
        ShopUser entity = selectOne(new CoreLambdaQueryWrapper<ShopUser>()
                .select(ShopUser::getShopId)
                .eq(ShopUser::getAdminUserId, adminUserId)
                .orderByDesc(ShopUser::getId));
        if (entity == null) {
            return null;
        }
        return entity.getShopId();
    }

    @DataPermission(enable = false) // 指定店铺编号查询，需忽略店铺的数据权限
    default List<ShopUser> selectList(Long userId, Integer userType, @Nullable Long shopId) {
        if (userId == null || userId <= 0) {
            return null;
        }
        if (userType == null) {
            userType = UserTypeEnum.MEMBER.getValue();
        }
        List<ShopUser> list = null;
        if (Objects.equals(userType, UserTypeEnum.MEMBER.getValue())) {
            list = selectList(new CoreLambdaQueryWrapper<ShopUser>()
                    .eq(ShopUser::getMemberUserId, userId)
                    .eqIfPresent(ShopUser::getShopId, shopId)
                    .orderByDesc(ShopUser::getId));
        } else if (Objects.equals(userType, UserTypeEnum.ADMIN.getValue())) {
            list = selectList(new CoreLambdaQueryWrapper<ShopUser>()
                    .eq(ShopUser::getAdminUserId, userId)
                    .eqIfPresent(ShopUser::getShopId, shopId)
                    .orderByDesc(ShopUser::getId));
        }
        return list;
    }

    @DataPermission(enable = false) // 指定店铺编号查询，需忽略店铺的数据权限
    default List<ShopUser> selectListByShopId(Long shopId) {
        return selectList(new CoreLambdaQueryWrapper<ShopUser>()
                .eqIfPresent(ShopUser::getShopId, shopId)
                .orderByDesc(ShopUser::getId));
    }

}
